package com.freesky.biz.rights.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.freesky.biz.rights.domain.RightsQuesEntity;
import com.freesky.biz.rights.domain.RightsQuesOptionEntity;
import com.freesky.biz.rights.service.IRightsQuesOptionService;
import com.freesky.biz.rights.service.IRightsQuesService;
import com.freesky.common.annotation.Log;
import com.freesky.common.core.controller.BaseController;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.page.TableDataInfo;
import com.freesky.common.enums.BusinessType;
import com.freesky.common.utils.SeqUtil;
import com.freesky.common.utils.StringUtils;
import com.freesky.common.utils.uuid.Seq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 题目选项Controller
 */
@RestController
@RequestMapping("/rights/quesOption")
public class RightsQuesOptionController extends BaseController {

    @Autowired
    private IRightsQuesOptionService quesOptionService;

    /**
     * 获取题目列表
     */
    @PreAuthorize("@ss.hasPermi('rights:quesOption:list')")
    @GetMapping("/list")
    public TableDataInfo list(RightsQuesOptionEntity rightsQuesOptionEntity) {
        LambdaQueryWrapper<RightsQuesOptionEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(RightsQuesOptionEntity::getOrderNum);
        wrapper.eq(RightsQuesOptionEntity::getDelFlag, "0");
        if(StringUtils.isNotEmpty(rightsQuesOptionEntity.getQuesId())){
            wrapper.eq(RightsQuesOptionEntity::getQuesId, rightsQuesOptionEntity.getQuesId());
        }
        IPage<RightsQuesOptionEntity> list = quesOptionService.page(plusPage(), wrapper);
        return getDataTable(list);
    }

    /**
     * 获取题目详细信息
     */
    @PreAuthorize("@ss.hasPermi('rights:quesOption:query')")
    @GetMapping(value = "/{quesId}")
    public AjaxResult getInfo(@PathVariable("quesId") String quesId) {
        //return success(quesService.getQuesWithOptions(quesId));
        return success(quesOptionService.getById(quesId));
    }

    /**
     * 新增题目
     */
    @PreAuthorize("@ss.hasPermi('rights:quesOption:add')")
    @Log(title = "题目管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody RightsQuesOptionEntity rightsQuesOption) {
        rightsQuesOption.setOptionId(SeqUtil.getSeqId());
        rightsQuesOption.setCreateBy(String.valueOf(getUserId()));
        rightsQuesOption.setCreateTime(new Date());
        rightsQuesOption.setDelFlag("0");
        return toAjax(quesOptionService.insertQuesOption(rightsQuesOption));
    }

    /**
     * 修改题目
     */
    @PreAuthorize("@ss.hasPermi('rights:quesOption:edit')")
    @Log(title = "题目管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody RightsQuesOptionEntity rightsQuesOption) {
        rightsQuesOption.setUpdateBy(String.valueOf(getUserId()));
        rightsQuesOption.setUpdateTime(new Date());
        rightsQuesOption.setDelFlag("0");
        return toAjax(quesOptionService.updateQuesOption(rightsQuesOption));
    }

    /**
     * 删除题目
     */
    @PreAuthorize("@ss.hasPermi('rights:quesOption:remove')")
    @Log(title = "题目管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{quesIds}")
    public AjaxResult remove(@PathVariable String[] quesIds) {
        return toAjax(quesOptionService.deleteQuesOption(quesIds));
    }
}
